<?php
/*
 * Installing program
 * Usage: php install.php
 * Create at: 12:19 2012/11/16
 * Author: leixun(lein_urg@163.com)
 */
if(php_sapi_name()!='cli')
{
	printUsage();
	die();
}
if(!checkEvioronment())
{
	printf('To enable the System V shared memory support compile PHP with the option %c[44m--enable-sysvshm%c[0m . 
To enable the System V messages support compile PHP with the option %c[44m--enable-sysvmsg%c[0m .
To enable the mysql support compile PHP with the option %c[44m--with-mysql[=DIR]%c[0m .
To enable the Process Control support compile PHP with the option %c[44m--enable-pcntl%c[0m .
',27,27,27,27,27,27,27,27);
	die();
}
$path = '';
while(!is_file($path))
{
	if(path!='') printf($path.' is not valid path, please enter the full path.%c',10);
	printf('Please enter the php bin path(similar as: /usr/bin/php or /usr/local/php/bin/php )%c%c[5m>%c[0m',10,27,27);
	$path = readline(""); 
}
exec("gcc -o cpf libevent2_1.c -levent",$output,$return_var);
if($return_var!=0)
{
	printf("%c[5;31mCompile socket server failed!%c[0m\nMake sure you have gcc tools in your system.\nIf you are using CentOS, try command: yum install -y gcc g++\n",27,27);
}else{
	$sh_content = '#!'.$path.'
'.file_get_contents('run.temp').'
run("'.$path.'",$service_file,$server_no);';
	if(!file_put_contents('run',$sh_content))
	{
		printf('%c[5;31mCreate file "run" failed! Make sure you have the permission of writing directory: '.dirname(__FILE__).'%c[0m\n',27,27);
	}else{
		printf('%c[5;32mCongratulations, the installation is complete%c[0m\n',27,27);
		printf('Use ./run server_no to start server.'.chr(10));
		printf('If you want to change socket server param, just modify file "run".'.chr(10));
		printf('Use ./cpf --help to see socket server param detail infomation'.chr(10));
	}
}


function compileSocketServer(){
	printf('%c[7mCompile socket server...%c[0m%c',27,27,10);
	if(!function_exists('exec'))
	{
		printf('%c[41mFunction exec is invalid\n%c[0m%c',27,27,10);
		return false;
	}
}
function checkEvioronment(){
	printf('%c[7mChecking envioronment...%c[0m%c',27,27,10);
	$mysql = $shm_attach = $msg_send = $pcntl_fork = false;

	print('System V messages support...');
	if(function_exists('msg_send'))
	{
		$msg_send = true;
		print('yes');
	}else print('no');
	printf('%cMysql support...',10);
	if(function_exists('mysql_connect'))
	{
		$mysql = true;
		print('yes');
	}else print('no');
	printf('%cSystem V shared memory support...',10);
	if(function_exists('shm_attach'))
	{
		$shm_attach = true;
		print('yes');
	}else print('no');
	printf('%cSystem V shared memory support...',10);
	if(function_exists('pcntl_fork'))
	{
		$pcntl_fork = true;
		print('yes');
	}else print('no');

	printf('%c', 10);
	return $mysql&&$shm_attach&&$msg_send&&$pcntl_fork;
}

function printUsage(){
	printf('%c[7mThis software is designed for CLI mode only!%c[0m
Please run such command under command line:php install.php',27,27);
}